package com.sky.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.sky.security.AuthUserDetails;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import com.alibaba.fastjson.annotation.JSONField;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Employee implements AuthUserDetails, Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;

    private String username;

    private String name;

    private String password;

    private String phone;

    private String sex;

    private String idNumber;

    private Integer status;

    // 简易角色id
    private Integer roleId;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    private LocalDateTime updateTime;

    private Long createUser;

    private Long updateUser;


    @JSONField(serialize = false)
    List<GrantedAuthority> authorities;

    //存储权限信息
    List<String> permissions;

    /**
     * 存储权限信息的集合
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (authorities != null) {
            return authorities;
        }
        if (permissions != null) {
            authorities = permissions.stream()
                    .map(SimpleGrantedAuthority::new)
                    .collect(Collectors.toList());
            return authorities;
        }
        return null;
    }

    /**
     * 用户是否启用
     */
    @Override
    public boolean isEnabled() {
        return this.status == 1;
    }

    /**
     * 简易获取角色名称
     * @param roleId
     * @return
     */
    public static String getRoleById(Integer roleId) {
        return roleId == 1 ? "超级管理员" : "普通管理员";
    }
}
